/**
  * 设置页面
  */
module game {

    export class SettingMediator extends BaseMediator {
        public static NAME: string = "SettingMediator";

        public constructor(viewComponent: any = null) {
            super(SettingMediator.NAME, viewComponent);
        }

        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.HALL.STAGE_RESIZE,
                PanelNotify.HALL.OPEN_SETTING,
                PanelNotify.HALL.CLOSE_SETTING
            ];
        }
        private panel: SettingPanel;
        private TrackWidth = 619; //这个值是皮肤上黄色进度条的原始长度，滑动音乐调节时根据此值*百分比计算新的长度

        public handleNotification(notification: puremvc.INotification): void {
            var data: any = notification.getBody();
            switch (notification.getName()) {
                case PanelNotify.HALL.STAGE_RESIZE:
                    if (this.panel) {
                        this.panel.width = GameConfig.curWidth()
                        this.panel.height = GameConfig.curHeight()
                    }
                    break;
                case PanelNotify.HALL.OPEN_SETTING:
                    //显示角色面板
                    this.panel = null;
                    this.panel = new SettingPanel();
                    this.showUI(this.panel, 2, 1, true);
                    break;
                case PanelNotify.HALL.CLOSE_SETTING:
                    this.closePanel(1);
                    break;
            }
        }

        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.panel.changeBtn.visible = false;
            this.panel.banbenhao.text = InstructionsEnum.banbenhao;
            this.panel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCloseBntClick, this);
            // this.panel.changeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onExitAccount, this);
            this.panel.slider_music.addEventListener(eui.UIEvent.CHANGE, this.onChangeYinYue, this);
            this.panel.slider_sound.addEventListener(eui.UIEvent.CHANGE, this.onChangeYinXiao, this);
            this.panel.slider_music.addEventListener(eui.UIEvent.CHANGE_END, this.onChangeYinYueEnd, this);
            this.panel.slider_sound.addEventListener(eui.UIEvent.CHANGE_END, this.onChangeYinXiaoEnd, this);
            this.panel.slider_music.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickYinYueEnd, this);
            this.panel.slider_sound.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onClickYinXiaoEnd, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {
            this.panel.slider_music.minimum = 0;//定义最小值
            this.panel.slider_music.maximum = 100;//定义最大值
            this.panel.slider_sound.minimum = 0;//定义最小值
            this.panel.slider_sound.maximum = 100;//定义最大值
            //初始化按钮
            var yinyueState = egret.localStorage.getItem("yinYueState");
            var yinxiaoState = egret.localStorage.getItem("yinXiaoState");
            var yinYuevolumeValue = egret.localStorage.getItem("yinYuevolumeValue");
            var yinXiaovolumeValue = egret.localStorage.getItem("yinXiaovolumeValue");
            this.panel.slider_music.value = Number(yinYuevolumeValue) * 100;
            this.panel.slider_sound.value = Number(yinXiaovolumeValue) * 100;
            this.addYinyueMask(Number(yinYuevolumeValue) * 100);
            this.addYinxiaoMask(Number(yinXiaovolumeValue) * 100)
        }
        //拖动音乐
        private onChangeYinYue(evt: eui.UIEvent): void {
            console.log(evt.target.value)
            this.addYinyueMask(evt.target.value);
            egret.localStorage.setItem("yinYuevolumeValue", evt.target.value / 100 + "");
            if (SoundManage.yinyueChannel != null) {
                SoundManage.yinyueChannel.volume = evt.target.value / 100;
            }
        }
        //拖动音乐结束
        private onChangeYinYueEnd(evt: eui.UIEvent) {
            if (evt != null) {
                if (evt.target.value < 5) {
                    egret.localStorage.setItem("yinYueState", "false");
                    SoundManage.stopYinYue();
                } else {
                    egret.localStorage.setItem("yinYueState", "true");
                    //如果声音是在停止状态则重新播放
                    if (SoundManage.yinyueChannel == null) {
                        egret.setTimeout(() => {
                            SoundManage.startYinYueLoad();
                        }, this, 100)
                    }
                }
            }
        }
        private onClickYinYueEnd(evt: egret.TouchEvent) {
            if (evt != null) {
                if (evt.target.value < 5) {
                    egret.localStorage.setItem("yinYueState", "false");
                    SoundManage.stopYinYue();
                } else {
                    egret.localStorage.setItem("yinYueState", "true");
                    //如果声音是在停止状态则重新播放
                    if (SoundManage.yinyueChannel == null) {
                        egret.setTimeout(() => {
                            SoundManage.startYinYueLoad();
                        }, this, 100)
                    }
                }
            }
        }
        private addYinyueMask(value: any) {
            this.panel.bgyyTrack.width = this.TrackWidth * (value / 100)
        }
        //拖动音效
        private onChangeYinXiao(evt: eui.UIEvent): void {
            this.addYinxiaoMask(evt.target.value)
            egret.localStorage.setItem("yinXiaovolumeValue", evt.target.value / 100 + "");
        }
        //拖动音效结束
        private onChangeYinXiaoEnd(evt: eui.UIEvent) {
            if (evt != null) {
                if (evt.target.value < 5) {
                    egret.localStorage.setItem("yinXiaoState", "false");
                } else {
                    egret.localStorage.setItem("yinXiaoState", "true");
                }
            }
        }
        // private onExitAccount(event: egret.TouchEvent) {
        //     SoundManage.startAnniuBtnLoad();
        //     EffectUtils.playDim(event.currentTarget);
        //     if (!GameVO.getVo().szMobilePhone) {
        //         let obj = {
        //             CODE: PanelNotify.LOGIN.EXIT_GAME,
        //             TEXT: "确定要切换账号吗？游客登陆将丢失账号！",
        //         }
        //         game.AppFacade.instance.sendNotification(PanelNotify.HALL.OPEN_TIP, obj);
        //     } else {
        //         // egret.localStorage.removeItem(InstructionsEnum.USERID);
        //         // egret.localStorage.removeItem(InstructionsEnum.USERPASSWORD);
        //         SocketManager.closeAllSocket()
        //         game.AppFacade.instance.sendNotification(PanelNotify.LOGIN.OPEN_MAIN);
        //     }

        // }
        //拖动音效结束
        private onClickYinXiaoEnd(evt: egret.TouchEvent) {
            if (evt != null) {
                if (evt.target.value < 5) {
                    egret.localStorage.setItem("yinXiaoState", "false");
                } else {
                    egret.localStorage.setItem("yinXiaoState", "true");
                }
            }
        }
        private addYinxiaoMask(value: any) {
            this.panel.yxyxTrack.width = this.TrackWidth * (value / 100);
        }

        private onCloseBntClick(event: egret.TouchEvent): void {
            SoundManage.startAnniuBtnLoad();
            this.closePanel(1);
        }

    }
}